package cc.linker.steplink.crm.task;


import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchanth5.service.acs.MerchantSalerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业绩结算任务
 *
 * @author huhj
 */
@Component
public class AchievementSettlementTask {


    @Autowired
    private MerchantSalerService merchantSalerService;

    @Async
    @RedisLock(value = "AchievementSettlementTask")
    @Scheduled(cron = "55 59 23 * * ? ")
    public void task1() {

        String strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String strStart = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 00:00:00";
        String strEnd = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 23:59:59";

        //获取所有加盟代理
        List<Map<String, Object>> allPromoterList = merchantSalerService.getAllPromoterList();
        for (Map<String, Object> da : allPromoterList) {
            String customerId = da.get("customerId").toString();
            String orgId = da.get("orgId").toString();
            Map<String, Object> param = new HashMap<>();
            param.put("orgId", orgId);
            param.put("customerId", customerId);
            param.put("startTime", strStart);
            param.put("endTime", strEnd);
            param.put("createDate", strNow);

            Integer sonCount = merchantSalerService.getTodaySonCount(param);//当天新增下级人数
            param.put("sonCount", sonCount);

            Integer orderCount = merchantSalerService.getSonOrderCount(param);
            param.put("orderCount", orderCount);

            Integer allRebate = merchantSalerService.getTodayAllRebate(param);//当天收到返润金额总额 单位分
            param.put("allRebate", allRebate);

            Integer serviceFee = merchantSalerService.getTodayServiceFee(param);//当天服务费 单位分
            param.put("serviceFee", serviceFee);

            Integer recommendFee = merchantSalerService.getTodayRecommendFee(param);//当天推荐费 单位分
            param.put("recommendFee", recommendFee);

            Long sonDealFee = merchantSalerService.getUserTotalVolume(customerId, 1, orgId, strStart, strEnd);//当天下级交易额 单位分
            param.put("sonDealFee", Integer.parseInt(sonDealFee.toString()));

            Long promoterDealFee = merchantSalerService.getUserTotalVolume(customerId, 2, orgId, strStart, strEnd);//当天下级代理交易额 单位分
            param.put("promoterDealFee", Integer.parseInt(promoterDealFee.toString()));

            Long authorFee = merchantSalerService.getAuthorFee(customerId, orgId, strStart, strEnd);
            param.put("authorFee",authorFee);

            merchantSalerService.saveAchievementSettlement(param);
        }


    }

}
